package com.neu.controller;

import com.neu.pojo.Dept;
import com.neu.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

@Controller
public class DeptController {

    @Autowired
    private DeptService deptService;


    /**
     * 分页不获取部门信息
     * @param page  第几页
     * @param limit  每页显示几条
     * @return
     */
    @RequestMapping("getDeptByPage")
    @ResponseBody
    public HashMap getDeptByPage(int page,int limit){

        /**
         * {"code":0,"msg":"数据获取成功","count":22,"data":[{},{}] }
         */
        List<Dept>  list = deptService.getDeptByPage(page,limit);
        int totalCount = deptService.getDeptCount();
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","success");
        map.put("count",totalCount);
        map.put("data",list);
        return map;
    }

    /**
     * 删除部门信息
     * @param id
     * @return
     */
    @RequestMapping("delDeptById")
    @ResponseBody
    public HashMap delDeptById(int id){
        int res = deptService.delDept(id);
        HashMap<String,Object> map = new HashMap<>();
        if(res>0) {
            map.put("code", 0);
            map.put("msg", "删除成功");
        }else{
            map.put("code", -1);
            map.put("msg", "删除失败，请稍后重试.");
        }
        return map;
    }

    @RequestMapping("editDept")
    @ResponseBody
    public HashMap editDept(Dept dept){
        int res = deptService.updateDept(dept);
        HashMap<String,Object> map = new HashMap<>();
        if(res>0) {
            map.put("code", 0);
            map.put("msg", "修改成功");
        }else{
            map.put("code", -1);
            map.put("msg", "修改失败，请稍后重试.");
        }
        return map;
    }

    @RequestMapping("addDept")
    @ResponseBody
    public HashMap addDept(Dept dept){
        int res = deptService.addDept(dept);
        HashMap<String,Object> map = new HashMap<>();
        if(res>0) {
            map.put("code", 0);
            map.put("msg", "新建成功");
        }else{
            map.put("code", -1);
            map.put("msg", "新建失败，请稍后重试.");
        }
        return map;
    }
}
